package com.cssy.xxkj.logic.web;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONArray;
import com.cssy.authority.service.IScDepartmentService;
import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.xxkj.attach.entity.AttachFileUploadDO;
import com.cssy.xxkj.attach.service.IAttachFileUploadService;
import com.cssy.xxkj.attach.util.CommConstants;
import com.cssy.xxkj.basic.constants.Constants;
import com.cssy.xxkj.basic.entity.BasicCompanyTestDO;
import com.cssy.xxkj.basic.entity.BasicMaterialsDO;
import com.cssy.xxkj.basic.entity.BasicMaterialsTypeDO;
import com.cssy.xxkj.basic.entity.BasicStapleUnitDO;
import com.cssy.xxkj.basic.entity.BasicStorageInfoTestDO;
import com.cssy.xxkj.basic.entity.BasicWorkshopDO;
import com.cssy.xxkj.basic.service.IBasicClerkService;
import com.cssy.xxkj.basic.service.IBasicCompanyTestService;
import com.cssy.xxkj.basic.service.IBasicCurrencyTestService;
import com.cssy.xxkj.basic.service.IBasicMaterialQualityService;
import com.cssy.xxkj.basic.service.IBasicMaterialsBomService;
import com.cssy.xxkj.basic.service.IBasicMaterialsService;
import com.cssy.xxkj.basic.service.IBasicMaterialsTypeService;
import com.cssy.xxkj.basic.service.IBasicModeDeliveryTestService;
import com.cssy.xxkj.basic.service.IBasicPersonnelService;
import com.cssy.xxkj.basic.service.IBasicProcessService;
import com.cssy.xxkj.basic.service.IBasicStorageInfoTestService;
import com.cssy.xxkj.basic.service.IBasicUtilService;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.contants.RoleUtil;
import com.cssy.xxkj.logic.dao.LogicSalesItemDAO;
import com.cssy.xxkj.logic.entity.LogicProductionStorageoutDO;
import com.cssy.xxkj.logic.entity.LogicPurchaseDO;
import com.cssy.xxkj.logic.entity.LogicPurchaseReturnDO;
import com.cssy.xxkj.logic.entity.LogicPurchaseWareDO;
import com.cssy.xxkj.logic.entity.LogicSalesDO;
import com.cssy.xxkj.logic.entity.LogicSalesItemDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseInventoryDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseMaterialoutItemDO;
import com.cssy.xxkj.logic.entity.vo.LogicCheckBackVo;
import com.cssy.xxkj.logic.entity.vo.UtilsEntityVO;
import com.cssy.xxkj.logic.service.ILogicCheckBackService;
import com.cssy.xxkj.logic.service.ILogicOutsideBoundService;
import com.cssy.xxkj.logic.service.ILogicOutsideStockService;
import com.cssy.xxkj.logic.service.ILogicProdDispatchingDetailService;
import com.cssy.xxkj.logic.service.ILogicProductionDispatchService;
import com.cssy.xxkj.logic.service.ILogicProductionDispatchingService;
import com.cssy.xxkj.logic.service.ILogicProductionGetService;
import com.cssy.xxkj.logic.service.ILogicProductionPlanService;
import com.cssy.xxkj.logic.service.ILogicPurchaseService;
import com.cssy.xxkj.logic.service.ILogicSalesService;
import com.cssy.xxkj.logic.service.ILogicUtilService;
import com.cssy.xxkj.logic.service.ILogicWarehouseInventoryService;
import com.cssy.xxkj.logic.service.IMaterialOutItemService;
import com.cssy.xxkj.logic.service.IMaterialOutService;
import com.cssy.xxkj.logic.util.OftenMapUtil;
import com.cssy.yd.logic.util.SqlUtil;
import com.plat.exception.SyBaseException;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.SessionUtil;
import com.plat.util.json.AjaxUtil;
import com.plat.util.json.OutJsonGrid;

@Controller
@RequestMapping("/public/windowalert")
public class WindowAlertController  {
	private static final String ITEM = "/public/ItemManager";// 选择物料
	private static final String AFFITEM = "/public/ItemAffList";// 选择物料
	private static final String CPITEM = "/public/CpItemManager";// 选择物料
	private static final String BATCHITEM = "/public/ItemBatchList";// 选择批次
	private static final String DEPTITEM = "/public/DeptList";//选择部分
	private static final String MATERIALSQUALITY = "/public/QualityList";// 选择材质
	private static final String PROCESS = "/public/ProcessList";// 选择工序
	private static final String PICODELIST = "/public/PiCodeList";// 选择PI编号
	private static final String BOMMATERIALlIST = "/public/materialBomList";//获得列表
	private static final String COMPANYLIST = "/public/CompanyList";// 选择客户
	private static final String CLERKLIST = "/public/ClerkList";// 选择业务员
	private static final String PLANLIST = "/public/PlanList";// 选择生产计划单
	private static final String PERSONNELLIST = "/public/PersonnelList";// 选择生产工作人员
	private static final String DISPATCHLIST = "/public/DispatchList";// 选择生产调度单
	private static final String DISPATCHINGLIST = "/public/DispatchingList";// 选择生产派工单
	private static final String DISPATCHINGLISTDETAIL = "/public/DispatchingDetailList";// 选择生产派工详情单
	private static final String SALESLIST = "/public/SalesList";//选择销售订单
	private static final String PROGETLIST = "/public/ProGetList";//选择生产领料单
	private static final String CURRENCY = "/public/Currency";// 选择生产货种
	private static final String PAYMODEL = "/public/PayModelList";//支付方式
	private static final String DISPATAOTRAGEINFO="public/Sotrageinfo";//选择产生仓库
	private static final String PURCHASERE = "/public/PurchaseList";// 选择采购单
	private static final String RETURNSINGLE = "/public/ReturnSingle";// 选择退货单
	private static final String PURCHASEWARE = "/public/PurchaseWare";// 选择生成采购入库单号
	private static final String ROLEWH = "/public/RoleWH";// 选择生成当前用户可以查看的仓库
	private static final String RECEPT = "/public/Recept";// 选择生成销售出库单
	private static final String MATERIALlIST="/public/materialListForPurchase";//采购订单预览
	private static final String OUTSIDEIN = "/public/OutSideInList";//选择委外入库单
	private static final String OUTSIDEOUT = "/public/OutSideOutList";//选择委外出库单
	private static final String OTHEROUT="/public/CheckBackList";//其他出库
	private static final String UPLOADFILE = "/public/UploadFileList";//附件列表
	private static final String BILLTYPESEL = "/public/BillTypeSelList";//入库单据选择
	
	@Resource(name="materialOutServiceImpl")
	private IMaterialOutService materialOutService;
	
	@Resource(name="basicMaterialsService")
	private IBasicMaterialsService basicMaterialsService;
	
	@Resource(name="basicMaterialsTypeService")
	private IBasicMaterialsTypeService basicmaterTypeService;
	
	@Resource(name="basicMaterialsBomService")
	private IBasicMaterialsBomService basicMaterialsBomService;
	
	@Resource(name="oftenMapUtil")
	private OftenMapUtil oftenMapUtil;
	
	@Resource(name="materialOutItemServiceImpl")
	private IMaterialOutItemService materialOutItemService ;
	
	@Resource(name = "logicWarehouseInventoryService")
	private ILogicWarehouseInventoryService logicWarehouseInventoryService;
	
	@Resource(name="scDepartmentService")
	private IScDepartmentService scDepartmentService;
	
	@Resource(name = "basicMaterialQualityService")
	private IBasicMaterialQualityService basicMaterialQualityService;
	
	@Resource(name = "basicProcessService")
	private IBasicProcessService basicProcessService;
	
	@Resource(name = "logicSalesService")
	private ILogicSalesService logicSalesService;
	
	@Resource(name = "basicCompanyTestService")
	private IBasicCompanyTestService basicCompanyTestService;
	
	@Resource(name = "basicClerkService")
	private IBasicClerkService basicClerkService;
	
	@Resource(name="basicStorageInfoTestService")
	private IBasicStorageInfoTestService basicStorageInfoTestService;

	@Resource(name = "logicProductionPlanService")
	private ILogicProductionPlanService logicProductionPlanService;
	
	@Resource(name="basicPersonnelService")
	private IBasicPersonnelService basicPersonnelService;
	
	@Resource(name = "logicProductionDispatchService")
	private ILogicProductionDispatchService logicProductionDispatchService;
	
	@Resource(name = "logicProductionDispatchingService")
	private ILogicProductionDispatchingService logicProductionDispatchingService;
	
	@Resource(name = "logicProdDispatchingDetailService")
	private ILogicProdDispatchingDetailService logicProdDispatchingDetailService;
	
	@Resource(name = "logicProductionGetService")
	private ILogicProductionGetService logicProductionGetService;

	@Resource(name = "basicCurrencyTestService")
	private IBasicCurrencyTestService basicCurrencyTestService;
	
	@Resource(name = "basicModeDeliveryTestService")
	private IBasicModeDeliveryTestService basicModeDeliveryTestService;
	
	@Resource(name = "basicUtilService")
	private IBasicUtilService basicUtilService;
	
	@Resource(name = "logicPurchaseService")
	private ILogicPurchaseService logicPurchaseService;
	
	@Resource(name = "logicUtilService")
	private ILogicUtilService logicUtilService;
	
	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;
	
	@Resource(name = "logicOutsideStockService")
	private ILogicOutsideStockService logicOutsideStockService;
	
	@Resource(name = "logicOutsideBoundService")
	private ILogicOutsideBoundService logicOutsideBoundService;
	
	@Resource(name = "logicCheckBackService")
	private ILogicCheckBackService logicCheckBackService;
	
	//附件
	@Resource(name = "attachFileUploadService")
    private IAttachFileUploadService attachFileUploadService;
	
	@Resource(name = "logicSalesItemDAO")
	private LogicSalesItemDAO logicSalesItemDAO;

	//查询其他出库单
	@RequestMapping("/checkback")
	public String otherOut(HttpServletRequest request, HttpServletResponse response,Model model){
		model.addAttribute("type", request.getParameter("type"));
		model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		return OTHEROUT;
	}
	@RequestMapping("/checkbackList")
	public void otherOutList(HttpServletRequest request, HttpServletResponse response){
		String type=request.getParameter("type");
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs.setRemovedProperty(null);
		List<LogicCheckBackVo> list=null;
		if(type.equals(BillStatusUtil.CHECK_BACK_OTHEROUT)){
			scs.addCondition("zd003BillStatus",BillStatusUtil.OTHERWH_BILL_STATUE_FOUR);
			list=logicCheckBackService.getCheckBackVoForLogicWareHouseOtherOut(scs);
		}else if(BillStatusUtil.CHECK_BACK_SALESOUT.equals(type)){
			scs.addCondition("zd011BillStatus",Arrays.asList("PROSTOROUT_END"));
			list=logicCheckBackService.getCheckBackVoForLogicProductionStorageoutDO(scs);
		}
		SearchResult rs=new SearchResult();
		rs.setRecordCount(list.size());
		rs.setResults(list);
		OutJsonGrid out = new OutJsonGrid(rs);
		out.printJSon(response);
		
	}
	//查询销售出库
	@RequestMapping(value = "/recept")
	public String recept(HttpServletRequest request, HttpServletResponse response)throws Exception{
		return RECEPT;
	}
	@RequestMapping("/receptList")
	public void receptList(Model model,HttpServletRequest request, HttpServletResponse response)throws Exception{
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs.setRemovedProperty(null);
		List<LogicProductionStorageoutDO> prodOutList = logicUtilService.resultStagout(scs);
		SearchResult rs=new SearchResult();
		rs.setRecordCount(prodOutList.size());
		rs.setResults(prodOutList);
		OutJsonGrid out = new OutJsonGrid(rs);
		out.printJSon(response);
	
	}
	/**
	 * 选择采购退货单号
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	
	@RequestMapping(value = "/returnSing")
	public String returnSing(HttpServletRequest request, HttpServletResponse response)throws Exception{
		return RETURNSINGLE;
	}
	
	
	@RequestMapping("/returnSingList")
	public void returnSingList(Model model,HttpServletRequest request, HttpServletResponse response)throws Exception{
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		//退货单的状态为：已审批完成的
		scs.addCondition("zd006Status", BillStatusUtil.PCSR_BILL_STATUS_FOUR, SearchCondition.OP_EQUAL);
		//退货单的付款状态为：未完全付款
		scs.addCondition("zd006PayStatus", BillStatusUtil.PCSR_PAY_BILL_STATUS_THREE, SearchCondition.OP_NOTEQUAL);
		List<LogicPurchaseReturnDO> pcsrList = logicUtilService.resultPurchaseReturn(scs);
		SearchResult rs= new SearchResult();
		rs.setRecordCount(pcsrList.size());
		rs.setResults(pcsrList);
		OutJsonGrid out = new OutJsonGrid(rs);
		out.printJSon(response);
	
	}
	
	/**
	 * 查询当前用户所有可见仓库
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	
	@RequestMapping(value = "/roleWH")
	public String roleWH(HttpServletRequest request, Model model)throws Exception{
		return ROLEWH;
	}
	@RequestMapping(value = "/roleWHList")
	public void roleWHList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		//List<BasicStorageInfoTestDO> list = SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService);
		Map<String,Object> sqlWSHMap = SqlUtil.sqlUtil.roleWhSqlFilter(request, scUserRolePlaceService,"zd002Code");
		scs.addCondition(sqlWSHMap.get("option")+"",(List<String>)sqlWSHMap.get("roleWhList"));
		SearchResult rs = basicStorageInfoTestService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(rs);
		out.printJSon(response);
	}
	
	/**
	 * 选择采购入库单号
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	
	@RequestMapping(value = "/pcsm")
	public String pcsm(HttpServletRequest request, Model model)throws Exception{
		//供应商id
		String superId = request.getParameter("superId");
		model.addAttribute("superId", superId);
		//采购订单id
		String pcsId = request.getParameter("pcsId");
		model.addAttribute("pcsId", pcsId);
		return PURCHASEWARE;
	}
	
	
	@RequestMapping("/pcsmItemList")
	public void pcsmItemList(Model model,HttpServletRequest request, HttpServletResponse response)throws Exception{
		//供应商id
		String superId = request.getParameter("superId");
		//采购订单id
		String pcsId = request.getParameter("pcsId");
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		SearchResult rs=new SearchResult();
		if(StringUtils.isNotBlank(superId)){
			scs.addCondition("zd008SupeId", superId, SearchCondition.OP_EQUAL);
		}
		//采购订单
		if(StringUtils.isNotBlank(pcsId)){
			scs.addCondition("zd001PcsId", pcsId, SearchCondition.OP_EQUAL);
		}
		//入库单状态
		scs.addCondition("zd004Status", BillStatusUtil.PCSW_BILL_STATUS_FOUR, SearchCondition.OP_EQUAL);
		if(StringUtils.isNotBlank(superId)){
			//付款状态
			scs.addCondition("zd004PayStatus", BillStatusUtil.PCSW_PAY_BILL_STATUS_THREE, SearchCondition.OP_NOTEQUAL);
		}
	    List<LogicPurchaseWareDO> list=	logicUtilService.resultPurchaseWare(scs);
	    rs.setRecordCount(list.size());
		rs.setResults(list);
		OutJsonGrid out = new OutJsonGrid(rs);
		out.printJSon(response);
	
	}
	
	
	
	
	/**
	 * 查询采购单
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	
	@RequestMapping(value = "/purchase")
	public String purchase(HttpServletRequest request, Model model)throws Exception{
		return PURCHASERE;
	}
	@RequestMapping(value = "/purchaseList")
	public void purchaseList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		//采购订单的状态为三审通过,且库存状态为未完全入库
		scs.addCondition("zd011Status", BillStatusUtil.PHS_BILL_STATUS_THREE, SearchCondition.OP_EQUAL);
		scs.addCondition("zd011WhStatus", BillStatusUtil.PCSWH_BILL_STATUS_TWO, SearchCondition.OP_NOTEQUAL);
		List<LogicPurchaseDO> list = logicUtilService.resultPurchase(scs);
		SearchResult rs=new SearchResult();
		rs.setRecordCount(list.size());
		rs.setResults(list);
		OutJsonGrid out = new OutJsonGrid(rs);
		out.printJSon(response);
	}
	
	/**
	 * 根据采购编号查询供应商
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/rscompany")
	public void rscompany(@RequestParam("purid") String purid, HttpServletResponse response)throws Exception{
					SearchConditions scs = new SearchConditions();
					scs.setRemovedProperty(null);
					LogicPurchaseDO pcr=logicPurchaseService.findListById(purid);
					scs.addCondition("id",pcr.getZd002SupeId(),SearchCondition.OP_EQUAL);
					List<BasicCompanyTestDO> bc = basicUtilService.resultCompany(scs);
					Map<String,Object> map = new HashMap<String, Object>();
					map.put("compay", bc.get(0));
					AjaxUtil.printJSonArr(response, JSONArray.toJSONString(map));
					
	}
	
	/**
	 * 查询仓库
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/sotrageinfo")
	public String sotrageinfo(HttpServletRequest request, Model model)throws Exception{
		String roleQuery = request.getParameter("roleQuery");
		if("y".equals(roleQuery)){
			model.addAttribute("roleQuery", "Y");
		}else{
			model.addAttribute("roleQuery", "N");
		}
		return DISPATAOTRAGEINFO;
	}
	@RequestMapping(value = "/sotrageinfolist")
	public void sotrageinfolist(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String roleQuery = request.getParameter("roleQuery");
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		if("Y".equals(roleQuery)){
			Map<String,Object> sqlWHMap = SqlUtil.sqlUtil.roleWhSqlFilter(request, scUserRolePlaceService,"zd002Code");
			scs.addCondition(sqlWHMap.get("option")+"",(List<String>)sqlWHMap.get("roleWhList"));
		}
		List<BasicStorageInfoTestDO> list = basicUtilService.resultStorage(scs);
		SearchResult rs=new SearchResult();
		rs.setRecordCount(list.size());
		rs.setResults(list);
		OutJsonGrid out = new OutJsonGrid(rs);
		out.printJSon(response);
	}
	
	
	
	/**
	 * 查找币种
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	
	@RequestMapping(value = "/currency")
	public String Currency(HttpServletRequest request, Model model)throws Exception{
		return CURRENCY;
	}
	@RequestMapping(value = "/currencylist")
	public void CurrencyList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs.setRemovedProperty("removed");
		SearchResult result=basicCurrencyTestService.findListByExample(scs);
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	/**
	 * 支付方式
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/paymodel")
	public String paymodel(HttpServletRequest request, Model model)throws Exception{
		return PAYMODEL;
	}
	
	@RequestMapping(value = "/paymodellist")
	public void paymodelList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs.setRemovedProperty("removed");
		SearchResult result=basicModeDeliveryTestService.findListByExample(scs);
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	/**
	 * 查找物料
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/itemmanager")
	public String itemmanager(HttpServletRequest request, Model model)
			throws Exception {
		model.addAttribute("ID",request.getParameter("ID"));
		return ITEM;
	}

	@RequestMapping(value = "/itemlist")
	public void ltemlist(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		if(scs.getOrderBy()==null||"".equals(scs.getOrderBy()))
			 scs.setOrderBy("zd001Code");
		scs.setRemovedProperty(null);
		String ID=request.getParameter("ID");
		if(ID!=null&&!"".equals(ID)){
			List<String> pIdList = new ArrayList<String>();
			pIdList.add(ID);
			SearchConditions pScs = new SearchConditions();
			pScs.addCondition("zd003ParentId", ID, SearchCondition.OP_EQUAL);
			List<BasicMaterialsTypeDO> pTypeList =basicmaterTypeService.findList(pScs);
			if(pTypeList==null){
				pTypeList = new ArrayList<BasicMaterialsTypeDO>();
			}
			for(BasicMaterialsTypeDO pType : pTypeList){
				if(pType==null||StringUtils.isBlank(pType.getId())){
					continue;
				}
				if(!pIdList.contains(pType.getId())){
					pIdList.add(pType.getId());
				}
			}
			scs.addCondition("zd029MaterTypeId",pIdList);
		}
		SearchResult result = basicMaterialsService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	
	/**
	 * 查找物料
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/cpitem")
	public String cpitem(HttpServletRequest request, Model model)
			throws Exception {
		return CPITEM;
	}

	@RequestMapping(value = "/cpitemlist")
	public void cpitemlist(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions materScs = new SearchConditions();
		hander.getWheres(materScs);
		if(materScs.getOrderBy()==null||"".equals(materScs.getOrderBy()))
			 materScs.setOrderBy("bm.zd001Code");
		SearchConditions materTypeScs = new SearchConditions();
		List<String> strlist = new ArrayList<String>();
		strlist.add("");
		strlist.add("0");
		materTypeScs.addCondition("zd003ParentId",strlist);
		materTypeScs.addCondition("zd002Name","成品",SearchCondition.OP_EQUAL);
		List<BasicMaterialsTypeDO> list =basicmaterTypeService.findList(materTypeScs);
		List<String> pidlist = new ArrayList<String>();
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			for(int i=0;i<list.size();i++){
				pidlist.add(list.get(i).getId());
			}
			pidlist.addAll(tree(pidlist));
		}
		materScs.addCondition("bm.zd029MaterTypeId", pidlist);
		materScs.setRemovedProperty("bm.removed");
		SearchResult result = basicMaterialsService.findExBomListByExample(materScs);
		/*scs.addCondition("zd029MaterTypeId", pidlist);
		SearchResult result = basicMaterialsService.findListByExample(scs);
		
		List<BasicMaterialsDO> materList=result.getResults();
		Map<String, BasicProductBomDO> materBomExMap = this.findExProductionBom();
		List<BasicMaterialsDO> materExBomList = new ArrayList<BasicMaterialsDO>();
		if(materList==null||materList.isEmpty()||materList.size()==0){
			materExBomList = new ArrayList<BasicMaterialsDO>();
		}else{
			for(BasicMaterialsDO mater : materList){
				if(mater==null||StringUtils.isBlank(mater.getId())){
					continue;
				}
				String keyStr = mater.getId();
				if(materBomExMap==null||materBomExMap.get(keyStr)==null){
					continue;
				}
				mater.setZd025OpenBatch(1);
				materExBomList.add(mater);
			}
		}
		result.setResults(materExBomList);*/
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	/*private Map<String, BasicProductBomDO> findExProductionBom(){
		SearchConditions bomExScs = new SearchConditions();
		SearchResult rs = basicMaterialsBomService.findListByExample(bomExScs);
		List<BasicProductBomDO> materBomList = rs.getResults();
		if(materBomList==null||materBomList.isEmpty()||materBomList.size()==0){
			return null;
		}else{
			Map<String, BasicProductBomDO> materBomEx = new HashMap<String, BasicProductBomDO>();
			for(BasicProductBomDO materBom : materBomList){
				if(materBom==null||StringUtils.isBlank(materBom.getId())){
					continue;
				}
				String keyStr = materBom.getZd008MaterialsId();
				materBomEx.put(keyStr, materBom);
			}
			
			
			return materBomEx;
		}
	}*/
	
	/**
     * 生成tree第一层数据
     **/
	private List<String>  tree(List<String> list) throws SyBaseException {
		List<String> idlist = new ArrayList<String>();
		for (int i = 0; i < list.size(); i++) {
			List<BasicMaterialsTypeDO> list1 =basicmaterTypeService.findParentId(list.get(i));
			if(list1!=null&&!list1.isEmpty()&&list1.size()>0){
				for(int k=0;k<list1.size();k++){
					idlist.add(list1.get(k).getId());
				}
				idlist.addAll(tree(idlist));
			}
			
		}
		return idlist;
	}

	
	/**
	 * 查找库存物料
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/itemaff")
	public String itemaff(HttpServletRequest request, Model model)
			throws Exception {
		String code = request.getParameter("code");
		model.addAttribute("code", code);
		return AFFITEM;
	}

	@RequestMapping(value = "/itemafflist")
	public void itemafflist(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String code = request.getParameter("code");
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs.setRemovedProperty("logic_warehouse_inventory.removed");
		if(StringUtils.isNotBlank(code)){//modify 2014.10.29 liubo 查询所有物料 golden 领料出库中是查询所有物料
			scs.addCondition("logic_warehouse_inventory.zd001WhCode", code, SearchCondition.OP_EQUAL);
		}
		if(StringUtils.isBlank(scs.getOrderBy())){
			scs.setOrderBy("logic_warehouse_inventory.zd009Quantity desc");
		}
		SearchResult result = logicWarehouseInventoryService.findForWareHouseStockByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	/**
	 * 查找物料
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/itembatch")
	public String itembatch(HttpServletRequest request, Model model)
			throws Exception {
		String maCode = request.getParameter("maCode");
		String whCode = request.getParameter("whCode");
		String hubCode = request.getParameter("hubCode");
		model.addAttribute("maCode", maCode);
		model.addAttribute("whCode", whCode);
		model.addAttribute("hubCode", hubCode);
		return BATCHITEM;
	}

	@RequestMapping(value = "/itembatchlist")
	public void itembatchlist(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String maCode = request.getParameter("maCode");
		String whCode = request.getParameter("whCode");
		String hubCode = request.getParameter("hubCode");
		Map<String, String> map=new HashMap<String, String>();
		if(maCode!=null&&!"".equals(maCode)){
			map.put("code", maCode);
		}
		if(whCode!=null&&!"".equals(whCode)){
			map.put("whcode", whCode);
		}
		if(hubCode!=null&&!"".equals(hubCode)){
			map.put("hubcode", hubCode);
		}
		SearchResult rs=new SearchResult();
		List<String> list=logicWarehouseInventoryService.findbatch(map);
		rs.setRecordCount(list.size());
		rs.setResults(list);
		OutJsonGrid out = new OutJsonGrid(rs);
		out.printJSon(response);
	}
	
	/**
	 * 部门
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/deptItem")
	public String deptItem(HttpServletRequest request, Model model)throws Exception {
		return DEPTITEM;
	}
	
	@RequestMapping(value = "/deptItemList")
	public void deptItemList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs.setRemovedProperty(null);
		SearchResult result = scDepartmentService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	/**
	 * 查找材质
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/materialsQuality")
	public String materialsQuality(HttpServletRequest request, Model model)
			throws Exception {
		return MATERIALSQUALITY;
	}

	@RequestMapping(value = "/materialsQualitylist")
	public void materialsQualitylist(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs.setRemovedProperty("removed");
		SearchResult result = basicMaterialQualityService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	/**
	 * 查找工序
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/process")
	public String process(HttpServletRequest request, Model model)
			throws Exception {
		String theExShop = request.getParameter("theExShop");
		if("Y".equals(theExShop)){
			model.addAttribute("theExShop", "Y");
		}else{
			model.addAttribute("theExShop", "N");
		}
		return PROCESS;
	}

	@RequestMapping(value = "/processlist")
	public void processList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String theExShop = request.getParameter("theExShop");
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		if("Y".equals(theExShop)){
			SearchConditions workShopScs = new SearchConditions();
			Map<String,Object> sqlSHMap = SqlUtil.sqlUtil.roleShSqlFilter(request, scUserRolePlaceService,"zd001Code");
			workShopScs.addCondition(sqlSHMap.get("option")+"",(List<String>)sqlSHMap.get("roleShList"));
			List<BasicWorkshopDO> workShopList = basicUtilService.resultWorkshop(workShopScs);
			if(workShopList!=null&&!workShopList.isEmpty()&&workShopList.size()>0){
				List<String> wkspIdList = new ArrayList<String>();
				for(BasicWorkshopDO workShop : workShopList){
					if(workShop==null||StringUtils.isBlank(workShop.getId())){
						continue;
					}
					wkspIdList.add(workShop.getId());
				}
				scs.addCondition("zd003WorkShop", wkspIdList);
			}
		}
		SearchResult result = basicProcessService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	/**
	 * 查找PI编号
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/picode")
	public String picode(HttpServletRequest request, Model model)
			throws Exception {
		model.addAttribute("code", request.getParameter("code"));
		model.addAttribute("status", request.getParameter("status"));
		model.addAttribute("customer", request.getParameter("customer"));
		return PICODELIST;
	}

	@RequestMapping(value = "/picodelist")
	public void picodelist(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String code =request.getParameter("code");
		String status =request.getParameter("status");
		String customer =request.getParameter("customer");
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		if(StringUtils.isNotBlank(code)){
			scs.addCondition("zd002PiCode", code, SearchCondition.OP_LIKE);
		}
		if(StringUtils.isNotBlank(customer)){
			scs.addCondition("zd005CustomerCode", customer, SearchCondition.OP_EQUAL);
		}
		if(StringUtils.isNotBlank(status)){
			scs.addCondition("zd024Billstatus", status, SearchCondition.OP_EQUAL);
		}else{
			scs.addCondition("zd024Billstatus", "PCSR_APPROVE", SearchCondition.OP_EQUAL);
		}
		//判断有没有admin的权限
		boolean adminEx = SessionUtil.getUserExRole(RoleUtil.ROLE_AD_DT, request);
		if(adminEx){
			List<String> saleType = Arrays.asList("MT","BT");
			scs.addCondition("zd028SaleType", saleType);
		}else{
			Map<String, String> jlczMap = SessionUtil.getUserExRoleToMap(RoleUtil.jlczList, request);
			if(jlczMap==null){
				scs.addCondition("zd028SaleType", "" , SearchCondition.OP_EQUAL);
			}else{
				if("y".equals(jlczMap.get(RoleUtil.ROLE_FT_DT))){
					scs.addCondition("zd028SaleType", "BT" , SearchCondition.OP_EQUAL);
				}else if("y".equals(jlczMap.get(RoleUtil.ROLE_MG_DT))){
					scs.addCondition("zd028SaleType", "MT" , SearchCondition.OP_EQUAL);
				}else{
					scs.addCondition("zd028SaleType", "" , SearchCondition.OP_EQUAL);
				}
			}
		}
		SearchResult result = logicSalesService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
	/**
	 * 根据PI编号，查询pi编号销售订单产品绑定bom的物料
	 * @param request
	 * @param model
	 */
	@RequestMapping(value="/getMaterialListByPicode")
	public String getMaterialListByPicode(HttpServletRequest request, HttpServletResponse response,
			Model model){
		String piCode = request.getParameter("piCode");
		model.addAttribute("piCode", piCode);
		String whId = request.getParameter("whId");
		model.addAttribute("whId", whId);
		return BOMMATERIALlIST;
	}
	
	@RequestMapping( value="/basicMaterialsItemBomService" )
	public void basicMaterialsItemBomService(HttpServletRequest request, HttpServletResponse response,
			Model model)throws Exception{
		try {
			String whId=request.getParameter("whId");
			String piCode=request.getParameter("piCode");
			//获取销售订单的ITEM
			List<LogicSalesItemDO> itemList = this.getSalesItem(piCode);
			//获取所有维护了bom的产品的物料 key为产品的编码
			Map<String, List<UtilsEntityVO>> materIdAndBomdIdMap =oftenMapUtil.getProductionIDAndBomIdAll();
			if(materIdAndBomdIdMap==null){
				throw new SyBaseException("没有维护BOM");
			}
			SearchConditions stoScs= new SearchConditions();
			//获取库存 key code name model spec color quanlity picode
			Map<String, LogicWarehouseInventoryDO> storageMap = getStorage(whId, stoScs);
			//获取以前的已出库的数量 key + picode
			Map<String, LogicWarehouseMaterialoutItemDO> outMap=this.getMaterialOutListItemMap(piCode, stoScs);
			//需要的map
			Map<String, LogicWarehouseMaterialoutItemDO> myNeedMap= new HashMap<String, LogicWarehouseMaterialoutItemDO>();
			//单位的map
			Map<String, BasicStapleUnitDO> utilMap=oftenMapUtil.getUtilAllMap();
			if(!(itemList.isEmpty()||itemList==null||itemList.size()==0)){
				for(LogicSalesItemDO item:itemList){
					if(item==null){
						continue;
					}
					List<UtilsEntityVO> materList=materIdAndBomdIdMap.get(item.getZd007MaterialsCode());
					if(materList==null){
						continue;
					}
					for(UtilsEntityVO vo:materList){// code name model spec  color quanlity
						String keyStr=(StringUtils.isEmpty(vo.getMaterialCode())?null:vo.getMaterialCode())+"_"+
										(StringUtils.isEmpty(vo.getMaterialName())?null:vo.getMaterialName())+"_"+
										(StringUtils.isEmpty(vo.getMaterialModel())?null:vo.getMaterialModel())+"_"+
										(StringUtils.isEmpty(vo.getMaterialSpec())?null:vo.getMaterialSpec())+"_"+
										(StringUtils.isEmpty(vo.getMaterialColor())?null:vo.getMaterialColor())+"_"+
										(StringUtils.isEmpty(vo.getMaterialQuanlity())?null:vo.getMaterialQuanlity());
						//获取以前的已出库的数量 key + 备货（picode golden在领料出库中 出库的物料全部是备货）
						LogicWarehouseInventoryDO sto=storageMap.get(keyStr+"_备货");
						//本次本pi订单中需要的物料
						LogicWarehouseMaterialoutItemDO mOut= myNeedMap.get(keyStr);
						if(mOut==null){
							mOut=new LogicWarehouseMaterialoutItemDO();
							mOut.setZd002MaterialsCode(StringUtils.isEmpty(vo.getMaterialCode())?null:vo.getMaterialCode());
							mOut.setZd002MaterialsName(StringUtils.isEmpty(vo.getMaterialName())?null:vo.getMaterialName());
							mOut.setZd004MaterialsModel(StringUtils.isEmpty(vo.getMaterialModel())?null:vo.getMaterialModel());
							mOut.setZd017MaterialsSize(StringUtils.isEmpty(vo.getMaterialSpec())?null:vo.getMaterialSpec());
							mOut.setZd016MaterialsColor(StringUtils.isEmpty(vo.getMaterialColor())?null:vo.getMaterialColor());
							mOut.setZd018MaterialsQuanlity(StringUtils.isEmpty(vo.getMaterialQuanlity())?null:vo.getMaterialQuanlity());
							mOut.setZd022MaterialBomQuantity((vo.getMaterialQuantity()==null?BigDecimal.ZERO:vo.getMaterialQuantity())
									.multiply(item.getZd013Quantity()==null?BigDecimal.ZERO:item.getZd013Quantity()).setScale(4));
							mOut.setZd006Unit(utilMap.get(vo.getMaterialCode())==null?vo.getMaterialUtil():utilMap.get(vo.getMaterialCode()).getZd002Name());
							mOut.setZd002MaterialsId(vo.getMaterialId());
						}else{
							//设置应该出库的数量 本次其他 在同一个订单中不同的产品 使用了同一个物料 两次
							BigDecimal mOutBQ=mOut.getZd022MaterialBomQuantity()==null?BigDecimal.ZERO:mOut.getZd022MaterialBomQuantity();
							BigDecimal voMQ=vo.getMaterialQuantity()==null?BigDecimal.ZERO:vo.getMaterialQuantity();
							BigDecimal itemQ=item.getZd013Quantity()==null?BigDecimal.ZERO:item.getZd013Quantity();
							mOut.setZd022MaterialBomQuantity(mOutBQ.add(voMQ.multiply(itemQ)).setScale(4));
						}
						//获取已出库数
						if(sto==null){
							mOut.setZd014WhQuantity(BigDecimal.ZERO);
							mOut.setZd009Price(BigDecimal.ZERO);
						}else{
							mOut.setZd009Price(sto.getZd024Price()==null?BigDecimal.ZERO:sto.getZd024Price());
							mOut.setZd014WhQuantity(sto.getZd009Quantity());
							
						}
						myNeedMap.put(keyStr, mOut);
					}
				}
				//获取输出的list
				List<LogicWarehouseMaterialoutItemDO> outListNow= new ArrayList<LogicWarehouseMaterialoutItemDO>();
				for(String key:myNeedMap.keySet()){
					LogicWarehouseMaterialoutItemDO outBefore=outMap.get(key);
					LogicWarehouseMaterialoutItemDO out=myNeedMap.get(key);
					//匹配已出库的数量
					if(outBefore!=null){
						out.setZd023MaterialBomOutQuantity((outBefore.getZd007Quantity().subtract(
								outBefore.getZd007BackQuantity()==null?BigDecimal.ZERO:outBefore.getZd007BackQuantity())));
					}else{
						out.setZd023MaterialBomOutQuantity(BigDecimal.ZERO);
					}
					out.setZd007Quantity((out.getZd022MaterialBomQuantity()==null?BigDecimal.ZERO:out.getZd022MaterialBomQuantity())
							.subtract(out.getZd023MaterialBomOutQuantity()==null?BigDecimal.ZERO:out.getZd023MaterialBomOutQuantity())
							.subtract(out.getZd007BackQuantity()==null?BigDecimal.ZERO:out.getZd007BackQuantity()));
					out.setZd021PiCode("备货");	
					outListNow.add(out);
				}
				SearchResult rs= new SearchResult();
				rs.setResults(outListNow);
				rs.setRecordCount(outListNow.size());
				OutJsonGrid out= new OutJsonGrid(rs);
				out.printJSon(response);
			}else{
				throw new Exception("销售订单中没有物品");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}
	private Map<String, LogicWarehouseMaterialoutItemDO>  getMaterialOutListItemMap(String piCode,
			SearchConditions stoScs) {
		stoScs.clearCondition();
		stoScs.setLimitClause(null);
		stoScs.setLimitPage(false);
		stoScs.addCondition("li.zd003BillStatus", BillStatusUtil.MATERIALOUT_BILL_STATUE_FOUR);
		stoScs.addCondition("li.zd004PICode", piCode);
		List<LogicWarehouseMaterialoutItemDO> outList= this.materialOutItemService.finListByCondition(stoScs);
		Map<String, LogicWarehouseMaterialoutItemDO> outMap= new HashMap<String, LogicWarehouseMaterialoutItemDO>();
		for(LogicWarehouseMaterialoutItemDO item:outList){// code name model spec color quantity
			String keyStr=(StringUtils.isEmpty(item.getZd002MaterialsCode())?null:item.getZd002MaterialsCode())+"_"+
							(StringUtils.isEmpty(item.getZd002MaterialsName())?null:item.getZd002MaterialsName())+"_"+
							(StringUtils.isEmpty(item.getZd004MaterialsModel())?null:item.getZd004MaterialsModel())+"_"+
							(StringUtils.isEmpty(item.getZd017MaterialsSize())?null:item.getZd017MaterialsSize())+"_"+
							(StringUtils.isEmpty(item.getZd016MaterialsColor())?null:item.getZd016MaterialsColor())+"_"+
							(StringUtils.isEmpty(item.getZd018MaterialsQuanlity())?null:item.getZd018MaterialsQuanlity());
			LogicWarehouseMaterialoutItemDO it=outMap.get(keyStr);
			//把以前所有的本PI的所有物料全部
			if(it!=null){
				item.setZd007Quantity(it.getZd007Quantity().add(item.getZd007Quantity()));
				item.setZd007BackQuantity((it.getZd007BackQuantity()==null?BigDecimal.ZERO:it.getZd007BackQuantity())
						.add(item.getZd007BackQuantity()==null?BigDecimal.ZERO:item.getZd007BackQuantity()));
				
			}
			outMap.put(keyStr, item);
		}
		return outMap;
	}
	private Map<String, LogicWarehouseInventoryDO> getStorage(String whId,
			SearchConditions stoScs) {
		if(StringUtils.isEmpty(whId)){
			throw new SyBaseException("仓库ID 为null");
		}
		stoScs.setLimitClause(null);
		stoScs.setLimitPage(false);
		stoScs.addCondition("zd001WhId", whId);
		//PIcode为备货
		stoScs.addCondition("zd026PiCode","备货");
		Map<String, LogicWarehouseInventoryDO> storageMap=oftenMapUtil.getStorageMap(stoScs);
		return storageMap;
	}
	private List<LogicSalesItemDO> getSalesItem(String piCode) {
		SearchConditions gcScs = new SearchConditions();
		gcScs.addCondition("zd002PiCode",piCode, SearchCondition.OP_EQUAL);
		List<String> biSts= Arrays.asList(BillStatusUtil.SES_STATUS_FOUR,BillStatusUtil.SES_STATUS_FIVE,
				BillStatusUtil.SES_STATUS_SEVEN);
		gcScs.addCondition("zd024Billstatus", biSts);
		List<LogicSalesDO> list = logicSalesService.findListByExample(gcScs).getResults();
		List<String> conList= new ArrayList<String>();
		for(LogicSalesDO s:list){
			conList.add(s.getId());
		}
		SearchConditions scs= new SearchConditions();
		scs.setRemovedProperty(null);
		scs.setLimitClause(null);
		scs.setLimitPage(false);
		scs.addCondition("zd001SalesId", conList);
		List<LogicSalesItemDO> listItem=this.logicSalesItemDAO.find(scs);
		return listItem;
	}

	
	@RequestMapping(value = "/piCodeMaterList")
	public void piCodeMaterList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String piCode = request.getParameter("piCode");
		String whId = request.getParameter("whId");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("piCode", piCode);
		//所有物料的id集合
		List<String> idlist = new ArrayList<String>();
		//查询所有的销售订单的item
		List<LogicSalesItemDO> gsItemList = logicSalesService.findSalesItemListByMap(paramMap);
		if(gsItemList!=null&&!gsItemList.isEmpty()&&gsItemList.size()>0){
			Map<String, Object> materMap = new HashMap<String, Object>();
			Map<String, List<String>> materIdMap = basicMaterialsBomService.findProductionBomMaterToMap(materMap);
			Map<String, String> bomMap = basicMaterialsBomService.findProductionBomToMap(materMap);
			for(LogicSalesItemDO gsItem : gsItemList){
				if(gsItem==null||StringUtils.isBlank(gsItem.getId())){
					continue;
				}
				String keyStr = gsItem.getZd006MaterialsId();
				if(bomMap==null||bomMap.get(keyStr)==null){
					//表示产品没有bom
				}else{
					String bomIdStr = bomMap.get(keyStr);
					if(materIdMap==null||materIdMap.get(bomIdStr)==null){
						//表示该bom没有维护物料
					}else{
						idlist.addAll(materIdMap.get(bomIdStr));
					}
				}
			}
		}
		List<BasicMaterialsDO> hsMaterlist = new ArrayList<BasicMaterialsDO>();
		if(idlist!=null&&!idlist.isEmpty()&&idlist.size()>0){
			Map<String, LogicWarehouseInventoryDO> hsMaterQuantityMap = this.findWareHsMaterQuantityToMap(whId);
			SearchConditions materScs = new SearchConditions();
			materScs.addCondition("id", idlist);
			hsMaterlist = basicMaterialsService.findListByExample(materScs).getResults();
			if(hsMaterlist!=null&&!hsMaterlist.isEmpty()&&hsMaterlist.size()>0){
				for(BasicMaterialsDO bsMater : hsMaterlist){
					if(bsMater==null||StringUtils.isBlank(bsMater.getId())){
						continue;
					}
					String keyStr = whId+"_"+bsMater.getId();
					if(hsMaterQuantityMap==null||hsMaterQuantityMap.get(keyStr)==null){
						bsMater.setZdQuantity(BigDecimal.ZERO);
					}else{
						LogicWarehouseInventoryDO lid=hsMaterQuantityMap.get(keyStr);
						bsMater.setZdQuantity(lid.getZd009Quantity());
						bsMater.setZd036PiCode(lid.getZd026PiCode());
						bsMater.setZd026Size(lid.getZd006MaterialsSpec());
						bsMater.setZd033Color(lid.getZd008MaterialsColor());
						bsMater.setZd034Quality(lid.getZd008MaterialsQuanlity());
					}
				}
			}
		}
		OutJsonGrid out = new OutJsonGrid(hsMaterlist);
		out.printJSon(response);
	}
	
	private Map<String, LogicWarehouseInventoryDO> findWareHsMaterQuantityToMap(String whId){
		SearchConditions whScs = new SearchConditions();
		whScs.setRemovedProperty(null);
		whScs.addCondition("zd001WhId", whId, SearchCondition.OP_EQUAL);
		List<LogicWarehouseInventoryDO> gwhsList = logicWarehouseInventoryService.findListByExample(whScs).getResults();
		if(gwhsList==null||gwhsList.isEmpty()||gwhsList.size()==0){
			return null;
		}else{
			Map<String, LogicWarehouseInventoryDO> materQuantityMap = new HashMap<String, LogicWarehouseInventoryDO>();
			for(LogicWarehouseInventoryDO gwhs : gwhsList){
				if(gwhs==null || StringUtils.isBlank(gwhs.getId())){
					continue;
				}
				String keyStr = whId + "_" + gwhs.getZd003MaterialsId();
				gwhs.setZd009Quantity(gwhs.getZd009Quantity()==null?BigDecimal.ZERO:gwhs.getZd009Quantity());
				materQuantityMap.put(keyStr, gwhs);
			}
			return materQuantityMap;
		}
	}
	
	/**
	 * 根据zdType查询客户、供应商
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/company")
	public String company(HttpServletRequest request, Model model)
			throws Exception {
		String zdType = request.getParameter("zdType");
		model.addAttribute("zdType", zdType);
		return COMPANYLIST;
	}

	@RequestMapping(value = "/companylist")
	public void companyList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String zdType = request.getParameter("zdType");
		if(StringUtils.isBlank(zdType)){
			zdType = Constants.COMPANY_TYPE_COMP;
		}else{
			zdType = Constants.COMPANY_TYPE_CUST;
		
		}
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs.setRemovedProperty("removed");
		scs.addCondition("zd032Type", zdType, SearchCondition.OP_EQUAL);
		SearchResult result = basicCompanyTestService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	/**
	 * 查找业务员
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/clerk")
	public String clerk(HttpServletRequest request, Model model)
			throws Exception {
		return CLERKLIST;
	}

	@RequestMapping(value = "/clerklist")
	public void clerkList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs.setRemovedProperty("removed");
		SearchResult result = basicClerkService.findListByExample(scs);
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	/**
	 * 查找生产计划单
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/plan")
	public String plan(HttpServletRequest request, Model model)
			throws Exception {
		model.addAttribute("type",request.getParameter("type"));
		return PLANLIST;
	}

	@RequestMapping(value = "/planlist")
	public void planList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs.setRemovedProperty("removed");
		String type =request.getParameter("type");
		if(StringUtils.isNotBlank(type)&&"1".equals(type)){
			List<Long>  l = new ArrayList<Long>();
			l.add(0l);
			l.add(1l);
			scs.addCondition("zd012WareStatus", l);
			List<String>  str = new ArrayList<String>();
			str.add(BillStatusUtil.PROPLAN_BILL_STATUS_FOUR);
			str.add(BillStatusUtil.PROPLAN_BILL_STATUS_THREE);
			scs.addCondition("zd008Billstatus", str);
		}else if(StringUtils.isNotBlank(type)&&"2".equals(type)){
			List<Long>  l = new ArrayList<Long>();
			l.add(0l);
			l.add(1l);
			scs.addCondition("zd013WhGetStatus", l);
			List<String>  str = new ArrayList<String>();
			str.add(BillStatusUtil.PROPLAN_BILL_STATUS_FOUR);
			str.add(BillStatusUtil.PROPLAN_BILL_STATUS_THREE);
			scs.addCondition("zd008Billstatus", str);
		}else{
			List<String>  str = new ArrayList<String>();
			str.add(BillStatusUtil.PROPLAN_BILL_STATUS_TWO);
			str.add(BillStatusUtil.PROPLAN_BILL_STATUS_THREE);
			scs.addCondition("zd008Billstatus", str);
		}
		boolean adminEx = SessionUtil.getUserExRole(RoleUtil.ROLE_AD_DT, request);
		if(adminEx){
			//是权限管理员
			List<String> oriType = Arrays.asList("BT","MT");
			scs.addCondition("zd002OriType", oriType);
		}else{
			Map<String, String> jlczMap = SessionUtil.getUserExRoleToMap(RoleUtil.jlczList, request);
			if(jlczMap==null){
				scs.addCondition("zd002OriType", "" , SearchCondition.OP_EQUAL);
			}else{
				if("y".equals(jlczMap.get(RoleUtil.ROLE_FT_DT))){
					scs.addCondition("zd002OriType", "BT" , SearchCondition.OP_EQUAL);
				}else if("y".equals(jlczMap.get(RoleUtil.ROLE_MG_DT))){
					scs.addCondition("zd002OriType", "MT" , SearchCondition.OP_EQUAL);
				}else{
					scs.addCondition("zd002OriType", "" , SearchCondition.OP_EQUAL);
				}
			}
		}
		SearchResult result = logicProductionPlanService.findListByExample(scs);
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	/**
	 * 查找生产工作人员
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/personnel")
	public String personnel(HttpServletRequest request, Model model)
			throws Exception {
		String wspQuery = request.getParameter("wspQuery");
		if("y".equals(wspQuery)){
			model.addAttribute("wspQuery", "Y");
		}else{
			model.addAttribute("wspQuery", "N");
		}
		return PERSONNELLIST;
	}

	@RequestMapping(value = "/personnellist")
	public void personnelList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String wspQuery = request.getParameter("wspQuery");
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		if("Y".equals(wspQuery)){
			Map<String, Object> sqlWpMap = SqlUtil.sqlUtil.roleShSqlFilter(request,scUserRolePlaceService, "");
			List<String> whpCodeList = (List<String>)sqlWpMap.get("roleShList");
			if(whpCodeList!=null&&!whpCodeList.isEmpty()&&whpCodeList.size()>0){
				Map<String, String> whpMap = this.findQueryWorkShopToMap();
				List<String> whpIdList = new ArrayList<String>();
				for(String whpCode : whpCodeList){
					if(StringUtils.isBlank(whpCode)||whpMap==null||whpMap.get(whpCode)==null){
						continue;
					}
					whpIdList.add(whpMap.get(whpCode));
				}
				scs.addCondition("zd003Dept", whpIdList);
			}
		}
		SearchResult result = basicPersonnelService.findListByExample(scs);
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	private Map<String, String> findQueryWorkShopToMap(){
		SearchConditions wpScs = new SearchConditions();
		List<BasicWorkshopDO> whpList = basicUtilService.resultWorkshop(wpScs);
		if(whpList==null||whpList.isEmpty()||whpList.size()==0){
			return null;
		}else{
			Map<String, String> whpMap = new HashMap<String,String>();
			for(BasicWorkshopDO whp : whpList){
				if(whp==null||StringUtils.isBlank(whp.getId())){
					continue;
				}
				whpMap.put(whp.getZd001Code(), whp.getId());
			}
			return whpMap;
		}
	}
	
	/**
	 * 查找生产调度单
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/dispatch")
	public String dispatch(HttpServletRequest request, Model model)
			throws Exception {
		return DISPATCHLIST;
	}

	@RequestMapping(value = "/dispatchlist")
	public void dispatchList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs.setRemovedProperty("removed");
		List<String>  str = new ArrayList<String>();
		str.add(BillStatusUtil.PRODISPATCH_BILL_STATUS_TWO);
		str.add(BillStatusUtil.PRODISPATCH_BILL_STATUS_THREE);
		scs.addCondition("zd008Billstatus", str);
		SearchResult result = logicProductionDispatchService.findListByExample(scs);
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	/**
	 * 查找生产派工单
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/dispatching")
	public String dispatching(HttpServletRequest request, Model model)
			throws Exception {
		model.addAttribute("type", request.getParameter("type"));
		return DISPATCHINGLIST;
	}

	@RequestMapping(value = "/dispatchinglist")
	public void dispatchingList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs.setRemovedProperty("removed");
		String type = request.getParameter("type");
		if("1".equals(type)){
			List<String>  str = new ArrayList<String>();
			//str.add(BillStatusUtil.PRODISPAT_BILL_STATUE_THREE);
			//str.add(BillStatusUtil.PRODISPAT_BILL_STATUE_FOUR);
			scs.addCondition("zd008Billstatus", str);
			scs.addCondition("zd010ReworkStauts", Arrays.asList(0l,1l));
		}else{
			List<String>  str = new ArrayList<String>();
			//str.add(BillStatusUtil.PRODISPAT_BILL_STATUE_TWO);
			//str.add(BillStatusUtil.PRODISPAT_BILL_STATUE_THREE);
			scs.addCondition("zd008Billstatus", str);
		}
		SearchResult result = logicProductionDispatchingService.findListByExample(scs);
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	/**
	 * 查找生产派工详情单
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/detail")
	public String detail(HttpServletRequest request, Model model)
			throws Exception {
		return DISPATCHINGLISTDETAIL;
	}

	@RequestMapping(value = "/detaillist")
	public void detaillist(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions dScs = new SearchConditions();
		hander.getWheres(dScs);
		dScs.addCondition("zd008WorkStatus", BillStatusUtil.PRODISPATDTL_BILL_STATUS_ZERO, SearchCondition.OP_EQUAL);
		Map<String,Object> sqlWSHMap = SqlUtil.sqlUtil.roleShSqlFilter(request, scUserRolePlaceService,"zd011WorkShopCode");
		dScs.addCondition(sqlWSHMap.get("option")+"",(List<String>)sqlWSHMap.get("roleShList"));
		SearchResult result = logicProdDispatchingDetailService.findListByExample(dScs);
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	/**
	 * 查找生产领料单号
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/progetcode")
	public String peoGetCode(HttpServletRequest request, Model model)
			throws Exception {
		return PROGETLIST;
	}

	@RequestMapping(value = "/progetlist")
	public void peoGetList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs.setRemovedProperty("removed");
		List<Long> l =Arrays.asList(0l,1l);
		scs.addCondition("zd017ReturnStastus", l);
		scs.addCondition("zd011BillStatus", BillStatusUtil.PROGET_STATUS_FIVE,SearchCondition.OP_EQUAL);
		SearchResult result = logicProductionGetService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	/**
	 * 查找销售出库单号
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/salescode")
	public String salesCode(HttpServletRequest request, Model model)
			throws Exception {
		return SALESLIST;
	}

	@RequestMapping(value = "/saleslist")
	public void salesList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		List<Long> l =Arrays.asList(0l,1l);
		scs.addCondition("zd025WareStatus", l);
		List<String> str =Arrays.asList(BillStatusUtil.SES_STATUS_FOUR,//生产 审核 生产中，生产完结
				BillStatusUtil.SES_STATUS_SEVEN,BillStatusUtil.SES_STATUS_FIVE);
		scs.addCondition("zd024Billstatus", str);
		SearchResult result = logicSalesService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	/**
	 * 查询委外入库单
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/outSideIn")
	public String outSideIn(HttpServletRequest request, Model model)
			throws Exception {
		return OUTSIDEIN;
	}
	@RequestMapping(value = "/outSideInList")
	public void outSideInList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		List<String> stList =Arrays.asList(BillStatusUtil.OUTSIDEIN_STATUS_FOUR);
		scs.addCondition("zd001Status", stList);
		SearchResult result = logicOutsideStockService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	/**
	 * 委外出库单
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/outSideOut")
	public String outSideOut(HttpServletRequest request, Model model)
			throws Exception{
		return OUTSIDEOUT;
	}
	@RequestMapping(value = "/outSideOutList")
	public void outSideOutList(HttpServletRequest request, HttpServletResponse response)
			throws Exception{
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		List<String> stList =Arrays.asList(BillStatusUtil.OUTSIDEOUT_STATUS_FOUR);
		scs.addCondition("zd001Status", stList);
		SearchResult result = logicOutsideBoundService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	@RequestMapping(value = "/uploadFile")
	public String uploadFile(HttpServletRequest request, Model model)
			throws Exception{
		String saleId = request.getParameter("saleId");
		model.addAttribute("saleId", saleId);
		return UPLOADFILE;
	}
	
	@RequestMapping(value = "/uploadFileList")
	public void uploadFileList(HttpServletRequest request, HttpServletResponse response)
			throws Exception{
		List<AttachFileUploadDO> upFileList = new ArrayList<AttachFileUploadDO>(); 
		String saleId = request.getParameter("saleId");
		if(StringUtils.isNotBlank(saleId)){
			Map<String,String> paramMap = new HashMap<String,String>();
			paramMap.put("projectId", saleId);
			paramMap.put("fileRelated", CommConstants.FILE_RELATED_YES);
			upFileList = attachFileUploadService.findAttachFileUploadDOs(paramMap);
		}
		OutJsonGrid out = new OutJsonGrid(upFileList);
		out.printJSon(response);
	}
	@RequestMapping("/materialListForPurchase")
	public String materialListForPurchase(HttpServletRequest request, HttpServletResponse response
			, Model model){
		model.addAttribute("id", request.getParameter("id"));
		return MATERIALlIST;
	}
	
	//入库单据选择
	@RequestMapping(value = "billTypeSel")
	public String billTypeSel(HttpServletRequest request,  Model model)
		throws Exception{
		String zdType = request.getParameter("zdType");
		model.addAttribute("zdType", zdType);
		List<BasicStorageInfoTestDO> whList= SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService);
		model.addAttribute("whList",whList);
		return BILLTYPESEL;
	}
	@RequestMapping(value = "/billTypeSelList")
	public void billTypeSelList(HttpServletRequest request, HttpServletResponse response)
		throws Exception{
		String zdType = request.getParameter("zdType");
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		if(StringUtils.isNotBlank(zdType)){
			scs.addCondition("gs.zd006BillType", zdType, SearchCondition.OP_EQUAL);
		}
		Map<String,Object> sqlWHMap = SqlUtil.sqlUtil.roleWhSqlFilter(request, scUserRolePlaceService,"gs.zd002WhCode");
		scs.addCondition(sqlWHMap.get("option")+"",(List<String>)sqlWHMap.get("roleWhList"));
		scs.setRemovedProperty(" gs.removed ");
		SearchResult result = logicWarehouseInventoryService.findSingleStockToBackSel(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
}
